package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    //添加检查组
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')") //必须要有add权限才会执行此方法
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){

        try {
            //调用service层方法
           checkGroupService.addCheckGroup(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    //按条件分页查询检查项
    @RequestMapping("/findByCondition")
    //@ResponseBody
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean){

        try {
            //此时调用service方法
            PageResult pageResult = checkGroupService.findByCondition(queryPageBean);
            return pageResult;
        }catch (Exception e){
            //返回结果对象
            e.printStackTrace();
            return null;
        }
    }

    //根据id查询检查组
    @RequestMapping("/findById/{id}")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findCheckGroup(@PathVariable Integer id){

        CheckGroup checkGroup=null;
        try {
            //调用service层方法
            checkGroup = checkGroupService.findCheckGroup(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }


    @RequestMapping("/findCheckItemIds/{id}")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findCheckItemIds(@PathVariable Integer id){
        List<Integer> list;
        try {
            //调用service层方法
             list= checkGroupService.findCheckItemIds(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);

    }

    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result updateCheckGroup(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            //调用service层方法
            checkGroupService.updateCheckGroup(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


    //根据id查询检查组
    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findAll(){

        try {
            //调用service层方法
            List<CheckGroup> list = checkGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }
}
